package autoschedule;

import alarm.Notice;
import java.awt.Color;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*
NAME:  Juan C. Garcia ID# 56469701
CERTIFICATION: I certify that this work is my own and that
               none of it is the work of any other person.
*/
public class NoticeDialog extends JDialog
{
   private JFrame dialogF;
   private JOptionPane optionPane;
   private JPanel panel;

    public NoticeDialog(JFrame frame, List<Notice> list, String user)
    {
	super(frame, "Reminders for " + user);
        dialogF = frame;
        ArrayList alist = new ArrayList();

        for(Notice n: list)
        {
            NoticeFormat nt = new NoticeFormat(n.getStartDate(),n.getMessage());
            alist.add(0,nt);
        }
        NoticeFormat [] n = (NoticeFormat[]) alist.toArray(new NoticeFormat[alist.size()]);

        if(n.length > 0)        //Creates dialog only if there are reminders
        {                       //to be displayed
            Toolkit.getDefaultToolkit().beep(); //Play alert sound
            panel = new JPanel();
            optionPane = new JOptionPane(n, JOptionPane.INFORMATION_MESSAGE);
            panel.add(optionPane);
            setContentPane(panel);

            final JButton OKButton = (JButton) ((Container) optionPane.getComponent(1)).getComponent(0);
            OKButton.setFocusable(false);
            OKButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialogF.dispose();
            }
            });

            setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
            addWindowListener(new WindowAdapter() {
                @Override
                    public void windowClosing(WindowEvent we) {
                            dialogF.setVisible(false);
                            dialogF.dispose();
                }
            });

            pack();
            setLocationRelativeTo(frame);
            setResizable(false);
            setVisible(true);
        }
    }

    private class NoticeFormat
    {
        private String message;
        private int hour;
        private int minutes;
        private String zone;

        public NoticeFormat(Date d, String m)
        {
            message = m;
            if(d.getHours() > 12)
            {
                hour = d.getHours() - 12;
                zone = " pm";
            }
            else
            {
                hour = d.getHours();
                zone = " am";
            }
            minutes = d.getMinutes();
            
        }

        @Override
        public String toString()
        {
        	String min;
        	if ( minutes == 0)
        	{
        		min = "00";
        	}
        	else
        	{
        		min = ""+minutes;
        	}
            String toShow = " " + message + " at  " + hour + ":" + min + " "+ zone;
            return toShow;
        }
    }
    
    public static void main(String [] args)
    {
        JFrame test = new JFrame();
        test.setSize(500,500);
        Notice test_note = new Notice(new Date(), new Date(), "HEHehehe", "Game");
        Notice test_note2 = new Notice(new Date(), new Date(), "HEHehehe", "Game");
        ArrayList<Notice> t = new ArrayList<Notice>(2);
        t.add(test_note);
        t.add(test_note2);
        NoticeDialog n = new NoticeDialog(test, t, "Juan");
       
    }
}
